package spynx.desl.runtime.impl;

import java.util.HashMap;
import java.util.Map;

import spynx.desl.runtime.IVariable;
import spynx.desl.runtime.IVariableFrame;

public class VariableFrame implements IVariableFrame {

	protected IVariableFrame parent;
	protected Map<String, IVariable> space = new HashMap<String, IVariable>();
	
	public VariableFrame(IVariableFrame parentFrame) {
		this.parent = parentFrame;
	}
	
	@Override
	public IVariableFrame getParent() {
		return parent;
	}

	@Override
	public IVariable getVariable(String name) {
		return space.containsKey(name) ? space.get(name) : 
					parent != null ? parent.getVariable(name) : null;
	}

	@Override
	public void addVariable(IVariable variable) {
		space.put(variable.getName(), variable);
	}

	
	@Override
	public void clearTransientVars() {
		
		for (Object varName: space.keySet().toArray()) {
			IVariable var = space.get(varName);
			if (!var.isPersist())
				space.remove(varName);
		}
	}

	
	@Override
	public void addVariable(String varName, IVariable variable) {
		space.put(varName, variable);
	}

}
